<?php

/**
 * Branch form.
 *
 * @package    Amawal Invest
 * @subpackage form
 * @author     Ameen Shoman<amin.shoman@gmail.com>
 */
class BranchForm extends BaseBranchForm
{
	public function configure()
	{
		$admin_form = new sfGuardUserAdminForm($this->getGuardUser());
		unset( $admin_form['branch_id'] );
		unset( $admin_form['user_id'] );
		unset( $admin_form['id'] );
		$this->embedForm('sf_guard_user' , $admin_form);
	}

	public function doSave($con = null){
		parent::doSave($con);
		$g_user = $this->getAdminForm()->save($con);
		$prof = $g_user->getProfile();
		$prof->setBranchId($this->getObject()->getId());
		$g_user->save();
		$prof->save();
	}

	public function bind(array $taintedValues = null, array $taintedFiles = null){
		parent::bind($taintedValues , $taintedFiles);
		$this->getAdminForm()->bind($taintedValues[$this->getAdminForm()->getName()] , $taintedFiles);
	}
	


	protected function getGuardUser()
	{
		//return null;
		$user_profiles = $this->getObject()->getUserProfilesJoinsfGuardUser();

		$user_profile = count($user_profiles) > 0 ? $user_profiles[0] : null;
		//var_dump($user_profile->getSfGuardUser());exit;
		return $user_profile ? $user_profile->getSfGuardUser() : null;
	}


	protected function getAdminForm(){
		return $this->getEmbeddedForm('sf_guard_user');
	}




}
